<?php

/**
 * removal actions.
 *
 * @package    sf_sandbox
 * @subpackage removal
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class removalActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $q = new Doctrine_Query();
    $q->select('r.*');
    $q->from('Removal r');
    $q->leftJoin('RemovalParticipate rp');
    $q->where('rp.user_id = ?', $this->getUser()->getGuardUser()->getId());
    $q->andWhere('rp.removal_id=r.id');
    $this->removals = $q->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new removalForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new removalForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($removal = Doctrine_Core::getTable('removal')->find(array($request->getParameter('id'))), sprintf('Object removal does not exist (%s).', $request->getParameter('id')));
    $this->form = new removalForm($removal);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($removal = Doctrine_Core::getTable('removal')->find(array($request->getParameter('id'))), sprintf('Object removal does not exist (%s).', $request->getParameter('id')));
    $this->form = new removalForm($removal);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($removal = Doctrine_Core::getTable('removal')->find(array($request->getParameter('id'))), sprintf('Object removal does not exist (%s).', $request->getParameter('id')));
    $removal->delete();

    $this->redirect('removal/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $removal = $form->save();
      $removalParticipate = new RemovalParticipate();
      $removalParticipate->setRemoval($removal);
      $removalParticipate->setUserId($this->getUser()->getGuardUser()->getId());
      $removalParticipate->save();
      $this->redirect('removal/index');
    }
  }
}
